package com.five.wisdomClass.controller;

import com.five.wisdomClass.bean.ResponseData;
import com.five.wisdomClass.entity.*;
import com.five.wisdomClass.service.IStudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.Session;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Tag(name="学生的基本操作")
@Slf4j
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/student")
public class StudentController extends BaseController{

    private final IStudentService studentService;

    @Operation(summary = "查看自己的同班同学")
    @GetMapping("/getclassmates")
    public ResponseData<Object> getClassMates(Integer currentPage, Integer size,String sNumber){
        if (sNumber.isEmpty()){
            return null;
        }
        return returnObject(studentService.getSpecializedClassMember(currentPage,size,sNumber));
    }

    @Operation(summary = "查看自己的个人信息")
    @GetMapping("/getselfinfo")
    public ResponseData<Object> getSelfInfo(String sNumber){
        if (sNumber.isEmpty()){
            return null;
        }
        return returnObject(studentService.getSelfInfo(sNumber));
    }

    @Operation(summary = "获取课程表")
    @GetMapping("/getschedule")
    public ResponseData<Object> getSchedule(String sNumber){

        if (sNumber.isEmpty()){
            return null;
        }
        // 构造课程集合将公共课程和选修课程一起返回
        List<Course> courseList = new ArrayList<>();
        List<Course> publicCourse = studentService.getPublicCourses(sNumber);
        List<Course> takeCourse = studentService.getTakeCourses(sNumber);
        if (publicCourse != null){
            courseList.addAll(publicCourse);
        }
        if (takeCourse != null){
            courseList.addAll(takeCourse);
        }
        return returnObject(courseList);
    }

    @Operation(summary = "获取课程成绩信息")
    @GetMapping("/getselfscores")
    public ResponseData<Object> getScores(String sNumber){
        if (sNumber.isEmpty()){
            return null;
        }
        return returnObject(studentService.getSelfScores(sNumber));
    }

    @Operation(summary = "添加个人计划")
    @PostMapping("/addplan")
    public ResponseData<Boolean> addPlan(@RequestBody SelfPlan selfPlan){
        if (selfPlan.getContext().isEmpty() || selfPlan.getDate() == null || selfPlan.getSNumber() == null){
            return null;
        }
        return returnBoolean(studentService.addPlan(selfPlan));
    }

    @Operation(summary = "删除个人计划")
    @DeleteMapping("/deleteplan")
    public ResponseData<Boolean> deletePlan(@RequestParam("id") Integer id){
        if (id == null){
            return null;
        }
        return returnBoolean(studentService.deletePlan(id));
    }

    @Operation(summary = "修改个人计划")
    @PutMapping("/editplan")
    public ResponseData<Boolean> deletePlan(String context,Integer id){
        if (context.isEmpty() || id == null ){
            return null;
        }
        return returnBoolean(studentService.editPlan(context,id));
    }

    @Operation(summary = "查看个人计划")
    @GetMapping("/getplans")
    public ResponseData<Object> getPlans(Integer currentPage, Integer size, String sNumber){
        if (sNumber.isEmpty()){
            return null;
        }
        return returnObject(studentService.getPlans(currentPage,size,sNumber));
    }

    @Operation(summary = "提交请假申请")
    @PostMapping("/submitleave")
    public ResponseData<Boolean> submitLeave(@RequestBody Leave leave){
        if (leave.getLeaveType().isEmpty() || leave.getSNumber().isEmpty() || leave.getContext().isEmpty() || leave.getContact().isEmpty()){
            return null;
        }
        log.error("学号"+leave.getSNumber()+"姓名："+leave.getSName());
        return returnBoolean(studentService.submitLeave(leave));
    }

    @Operation(summary = "查看请假列表")
    @GetMapping("/getleaves")
    public ResponseData<Object> getLeaves(Integer currentPage, Integer size, String sNumber){
        if (sNumber.isEmpty()){
            return null;
        }
        return returnObject(studentService.getSelfLeaves(currentPage,size,sNumber));
    }

    @Operation(summary = "获取所学课程")
    @GetMapping("/getclasses")
    public ResponseData<Object> getClasses(String sNumber){
        if (sNumber.isEmpty()){
            return null;
        }
        // 构造课程集合将公共课程和选修课程一起返回
        List<Course> courseList = new ArrayList<>();
        List<Course> publicCourse = studentService.getSelfPublicCourses(sNumber);
        List<Course> takeCourse = studentService.getSelfTakeCourses(sNumber);
        if (publicCourse != null){
            courseList.addAll(publicCourse);
        }
        if (takeCourse != null){
            courseList.addAll(takeCourse);
        }
        return returnObject(courseList);
    }

    @Operation(summary = "查看课程签到信息")
    @GetMapping("/getsigninfo")
    public ResponseData<Object> getSignInfo(Integer currentPage, Integer size,String courseNumber,String sNumber){
        if (courseNumber.isEmpty() || sNumber.isEmpty()){
            return null;
        }
        return returnObject(studentService.getSignInfo(currentPage,size,courseNumber,sNumber));
    }

    @Operation(summary = "签到")
    @PutMapping("/sign")
    public ResponseData<Boolean> sign(@RequestBody SignInfo signInfo){
        if (signInfo.getSNumber().isEmpty()){
            return null;
        }
        return returnBoolean(studentService.sign(signInfo));
    }
    @Operation(summary = "签到超时")
    @PutMapping("/unsign")
    public ResponseData<Boolean> unSign(@RequestBody SignInfo signInfo){
        return returnBoolean(studentService.unSign(signInfo));
    }
}
